<?php
namespace app\video\controller;

use app\common\model\BanjiModel;
use cmf\controller\AdminBaseController;
use app\common\model\VideoModel;

class VideoController extends AdminBaseController
{
    public function index()
    {
        $name           = input('name');
        $class_id       = input('class_id');
        $task           = input('task');
        $start_time     = input('start_time');
        $end_time       = input('end_time');
        $where          = [];

        if ($class_id) {
            $where[]          = ['a.class_id', '=', $class_id];
        }

        if ($name) {
            $where[]          = ['a.name', 'like', $name.'%'];
        }
        if ($task) {
            $where[]          = ['a.task', 'like', $task.'%'];
        }
        if (!empty($start_time) && !empty($end_time)) {
            $start_time_num = strtotime($start_time);
            $end_time_num   = strtotime($end_time) + 86399;
            $where[]        = ['a.create_time', 'between', [$start_time_num, $end_time_num]];
        }
        $total_count = VideoModel::alias('a')->where($where)->count();
        $list        = VideoModel::alias('a')
            ->leftJoin(BanjiModel::tableName('b'), 'a.class_id=b.id')
            ->where($where)
            ->field('a.*,b.name as class_name')
            ->order('a.id DESC')
            ->paginate(page_config(), $total_count)->each(function ($item){
                $item['url'] = cmf_get_image_url($item['url']);
            });

        $this->assign([
            'list'             => $list,
            'page'             => $list->render(),
            'class_id'         => $class_id,
            'name'             => $name,
            'task'             => $task,
            'start_time'       => $start_time,
            'end_time'         => $end_time,
            'class'            => BanjiModel::column('name','id'),
        ]);

        return $this->fetch();
    }


    public function delete(){
        $id             = $this->request->param('id');
        $info =  VideoModel::where(['id'=>$id])->find();

        if(file_exists('/www/wwwroot/teach/public/'.$info['url'])){
            unlink('/www/wwwroot/teach/public/'.$info['url']);
        }
        /*dump($info['url']);
        dump(file_exists('/www/wwwroot/teach/public/'.$info['url']));die;*/
        VideoModel::where(['id'=>$id])->delete();
        $msg =  '删除成功';
        $this->success($msg, '');
    }
}